﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Multithreading
{
    class Program
    {
        public static void Main()
        {
             bool stopped = false;
             Thread t = new Thread(new ThreadStart(() => 
             {                
             while (!stopped)
             {
                    Console.WriteLine("Running...");
                    Thread.Sleep(1000);
                }
             }));
            
            t.Start();
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
            stopped  = true;
            t.Join();
        }


        public static void ThreadMethod(object o)
        {
            for (int i = 0; i < (int)o; i++)
            {
                Console.WriteLine("ThreadProc: {0}", i);
                Thread.Sleep(0);
            }
        }

        /**
         *  In this case, the thread is initialized with a lambda expression (which in turn is just a shorthand
            version of a delegate). The thread keeps running until stopped becomes true. After that,
            the t.Join method causes the console application to wait till the thread finishes execution.
            A thread has its own call stack that stores all the methods that are executed. Local variables
            are stored on the call stack and are private to the thread.
         * 
         **/

    }
}
